#!/bin/bash

usage(){
    U1="usage: $0 [-setcookie <cookie>] [-nocookie] "
    U2="[-nettick <tick>] [-win] [-vsn <OTP version>]"
    echo $U1 $U2
    exit
}

if [ ${0:0:1} == "/" ];then
    SPATH=$0
elif [ -x $PWD/$0 ];then
    SPATH=$PWD/$0
else
    SPATH=`which $0`
fi

n=4
while [ 0 -lt "$n" ]; do SPATH=`dirname $SPATH`; let n=n-1; done
PATHS=""
for p in `ls -d $SPATH/*/ebin`; do
    PATHS=$PATHS" -pa $p"
done

detach="-detached"
VSN=""
START="-s gperf ni"
HALT="-s erlang halt"
XTRA=""

while [ -n "$1" ] 
  do
  case $1 in
      "-help"|"-h"|"-?")
	  usage
	  ;;
      "-win")
	  WIN="xterm -sb -sl 9999 -bg gold -fg black -e"
          START="-s gperf start"
          HALT=""
	  detach=""
	  ;;
      "-dbg")
          START="-s gperf start"
          HALT=""
	  detach=""
	  ;;
      "-vsn"|"-version")
	  VSN="+R "$2
	  shift
	  ;;
      "-nettick"|"-tick")
	  nettick="-kernel net_ticktime $2"
	  shift 
	  ;;
      "-cookie"|"-setcookie")
	  cookie="-setcookie $2"
	  shift
	  ;;
      "-nocookie")
	  cookie=""
	  ;;
      "-*")
          echo dollar_one $1
          ;;
      *)
          XTRA="$1" $XTRA
          ;;
  esac
  shift
done

name="gperf_"$$
FLAGS="$detach -boot start_sasl -sasl errlog_type error +A16"
DISTR="-hidden -sname $name $cookie $nettick"

$WIN erl $VSN $FLAGS $DISTR $PATHS $START "$XTRA" $HALT
